//
//  FileReceive.m
//  FileReceive
//
//  Created by cr on 2021/3/24.
//

#import <Foundation/Foundation.h>
#import <React/RCTEventEmitter.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MobPush/MobPush.h>
#import <MOBFoundation/MobSDK.h>
#import <MOBFoundation/MobSDK+Privacy.h>
@interface LockMessageModule : RCTEventEmitter<RCTBridgeModule>

@end

@implementation LockMessageModule

RCT_EXPORT_MODULE()

+(BOOL)requiresMainQueueSetup
{
    return YES;
}
RCT_EXPORT_METHOD(setAlias:(NSDictionary*)params)
{

    NSString* alias =  [params valueForKey:@"idCode"];
    [MobPush setAlias:alias result:^(NSError *error) {
        
    }];
}
RCT_EXPORT_METHOD(sendMessage:(NSDictionary*)params)
{
    
}
RCT_EXPORT_METHOD(deleteAlias)
{
    [MobPush deleteAlias:^(NSError *error) {
    }];
    

}
+ (BOOL)isBlankString:(NSString *)string{
    
    if (string == nil) {
        
        return YES;
        
    }
    
    if (string == NULL) {
        
        return YES;
        
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        return YES;
        
    }
    
    return NO;
    
}
RCT_EXPORT_METHOD(checkPush)
{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* data =  [defaults stringForKey:@"schemeData"];
    
    BOOL isBlank =  [LockMessageModule isBlankString:data];
    if(!isBlank)
    {
        [self sendEventWithName:@"LockMessageModule"
                           body:@{@"code":@(0),
                                  @"content":data}];
        NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
        [defaults1 setValue:@"" forKey:@"schemeData"];
        [defaults1 synchronize];
    }

}
RCT_EXPORT_METHOD(clearLocalNotifications)
{
    

}
RCT_EXPORT_METHOD(submitPolicy)
{
    [MobSDK uploadPrivacyPermissionStatus:YES onResult:^(BOOL success) {
        NSLog(@"-------------->上传结果：%d",success);
    }];
    

}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"LockMessageModule"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];
    }
    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMessage:(NSNotification *)notification
{
    MPushMessage *message = notification.object;
   
        NSLog(@"message1:%@", message.convertDictionary);
        
        switch (message.messageType)
        {
            case MPushMessageTypeCustom:
            {// 自定义消息回调
                
            }
                break;
            case MPushMessageTypeAPNs:
            {// APNs回调
      
            }
                break;
            case MPushMessageTypeLocal:
            {// 本地通知回调
              
            }
                break;
            case MPushMessageTypeClicked:
            {// 点击通知回调
                NSDictionary* userInfo = message.notification.userInfo;
                NSString* data = userInfo[@"schemeData"];
                
                [self sendEventWithName:@"LockMessageModule"
                                   body:@{@"code":@(1),
                                          @"content":data}];
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setValue:data forKey:@"schemeData"];
                [defaults synchronize];
            }
            default:
                break;
        }
}
@end
